package view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.ParseException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
import model.Interim;
import model.Lab_Student;

/**
 *
 * @author cska
 *
 */

public class InterimDlg extends JDialog implements ocDialog {

    protected Interim interim;
    public boolean fl;
    protected GridBagConstraints c;
    protected JLabel l1, l2;
    protected JFormattedTextField date;
    protected JTextField mark;
    protected JPanel bp;
    protected JButton ok, cancel;

    public InterimDlg(JFrame owner, boolean modal, String title, Interim interim, Lab_Student ls) {
        super(owner, title, modal);
        if (interim != null)
            this.interim = interim;
        else
            this.interim = new Interim(ls, "", "");

        setLayout(new GridBagLayout());
        c = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        l1 = new JLabel("Дата:", JLabel.RIGHT);
        add(l1,c);
        c.gridy = 1;
        l2 = new JLabel("Причина:", JLabel.RIGHT);
        add(l2,c);

        MaskFormatter mask = null;
        try {
            mask = new MaskFormatter("####.##.##");
        } catch (ParseException ex) {
            Logger.getLogger(TeacherWnd.class.getName()).log(Level.SEVERE, null, ex);
        }
        date = new JFormattedTextField(mask);
        date.setHorizontalAlignment(JFormattedTextField.CENTER);
        if (this.interim.getDate().equals(""))
            setDateField(date);
        else
            date.setText(this.interim.getDate());
        c.gridx = 1;
        c.gridy = 0;
        c.weightx = 1.0;
        add(date,c);
        c.gridy = 1;
        mark = new JTextField(this.interim.getMark());
        mark.setHorizontalAlignment(JTextField.CENTER);
        add(mark,c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 2;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.BOTH;
        bp = new JPanel(new GridBagLayout());
        add(bp,c);
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.CENTER;
        ok = new JButton("Сохранить");
        ok.addActionListener(new okAdapter(this));
        bp.add(ok,c);
        c.gridx = 1;
        c.weightx = 0.0;
        cancel = new JButton("Отмена");
        cancel.addActionListener(new cancelAdapter(this));
        bp.add(cancel,c);

        setSize(350,150);
        Dimension screen = getToolkit().getScreenSize().getSize();
        setLocation((screen.width-350)/2, (screen.height-150)/2);
        setResizable(false);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setVisible(true);
    }

    public void ok() {
        fl = true;
        interim.setDate(date.getText());
        interim.setMark(mark.getText());
        setVisible(false);
    }

    public void cancel() {
        fl = false;
        setVisible(false);
    }

    public Interim getInterim() {
        return interim;
    }

    public void setDateField(JFormattedTextField field) {
        Calendar cal = Calendar.getInstance();
        cal = Calendar.getInstance();
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int month = (1 + cal.get(Calendar.MONTH));
        int year = cal.get(Calendar.YEAR);
        String date_str = year + "." + (month < 10 ? "0" + month : month ) + "." + (day < 10 ? "0" + day : day);
        field.setText(date_str);
    }

}
